﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MoroMoro.Samples.ToDoList.Application;
using MoroMoro.Samples.ToDoList.Domain.ToDos;
using MoroMoro.Samples.ToDoList.Domain.Categories;
using MoroMoro.Samples.ToDoList.Presentation.Models.ToDos;
using MoroMoro.Samples.ToDoList.Presentation.Presenters;
using MoroMoro.Samples.ToDoList.Presentation.Views.ToDos;
using MoroMoro.Samples.ToDoList.Presentation.Views.Categories;

namespace MoroMoro.Samples.ToDoList.Presentation.Views
{
    /// <summary>
    /// ToDoList フォームです。
    /// </summary>
    public partial class ToDoListForm : MyForm, IToDoListView
    {
        /// <summary>
        /// <see cref="ToDoListForm"/> クラスの新しいインスタンスを初期化します。
        /// </summary>
        /// <param name="facade">ファサード。</param>
        /// <exception cref="System.ArgumentNullException">facade が null です。</exception>
        public ToDoListForm(ToDoListFacade facade)
        {
            InitializeComponent();

            _presenter = new ToDoListPresenter(this, facade);
        }


        /// <summary>
        /// プレゼンター。
        /// </summary>
        private readonly ToDoListPresenter _presenter;


        #region Events

        /// <inheritdoc/>
        event EventHandler IToDoListView.StateComboBoxSelectedIndexChanged
        {
            add
            {
                _stateToolStripComboBox.SelectedIndexChanged += value;
            }
            remove
            {
                _stateToolStripComboBox.SelectedIndexChanged -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.CategoryComboBoxSelectedIndexChanged
        {
            add
            {
                _categoryToolStripComboBox.SelectedIndexChanged += value;
            }
            remove
            {
                _categoryToolStripComboBox.SelectedIndexChanged -= value;
            }
        }


        /// <inheritdoc/>
        event EventHandler IToDoListView.CreationButtonClick
        {
            add
            {
                _creationToolStripButton.Click += value;
            }
            remove
            {
                _creationToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.EditionButtonClick
        {
            add
            {
                _editionToolStripButton.Click += value;
            }
            remove
            {
                _editionToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.DeletionButtonClick
        {
            add
            {
                _deletionToolStripButton.Click += value;
            }
            remove
            {
                _deletionToolStripButton.Click -= value;
            }
        }


        /// <inheritdoc/>
        event EventHandler IToDoListView.ForwadToTouchedButtonClick
        {
            add
            {
                _forwardToTouchedToolStripButton.Click += value;
            }
            remove
            {
                _forwardToTouchedToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.ForwadToCompletedButtonClick
        {
            add
            {
                _forwardToCompletedToolStripButton.Click += value;
            }
            remove
            {
                _forwardToCompletedToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.ReversionToUntouchedButtonClick
        {
            add
            {
                _reversionToUntouchedToolStripButton.Click += value;
            }
            remove
            {
                _reversionToUntouchedToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.ReversionToTouchedButtonClick
        {
            add
            {
                _reversionToTouchedToolStripButton.Click += value;
            }
            remove
            {
                _reversionToTouchedToolStripButton.Click -= value;
            }
        }


        /// <inheritdoc/>
        event EventHandler IToDoListView.CategoryCreationButtonClick
        {
            add
            {
                _categoryCreationToolStripButton.Click += value;
            }
            remove
            {
                _categoryCreationToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.CategoryEditionButtonClick
        {
            add
            {
                _categoryEditionToolStripButton.Click += value;
            }
            remove
            {
                _categoryEditionToolStripButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoListView.CategoryDeletionButtonClick
        {
            add
            {
                _categoryDeletionToolStripButton.Click += value;
            }
            remove
            {
                _categoryDeletionToolStripButton.Click -= value;
            }
        }

        #endregion

        #region Properties

        /// <inheritdoc/>
        IEnumerable<ToDo> IToDoListView.CheckedItems
        {
            get
            {
                var result = 
                    from lvItem in _toDoListView.CheckedItems.Cast<ListViewItem>()
                    select lvItem.Tag as ToDo;
                return result;
            }
        }

        /// <inheritdoc/>
        ToDoState? IToDoListView.SelectedStateFilter
        {
            get
            {
                return (ToDoState?)_stateToolStripComboBox.ComboBox.SelectedValue;
            }
            set
            {
                if (value == null)
                {
                    _stateToolStripComboBox.ComboBox.SelectedIndex = 0;
                }
                else
                {
                    _stateToolStripComboBox.ComboBox.SelectedValue = value;
                }
            }
        }

        /// <inheritdoc/>
        Category IToDoListView.SelectedCategory
        {
            get
            {
                return (Category)_categoryToolStripComboBox.ComboBox.SelectedValue;
            }
            set
            {
                if (value == null)
                {
                    _categoryToolStripComboBox.ComboBox.SelectedIndex = 0;
                }
                else
                {
                    _categoryToolStripComboBox.ComboBox.SelectedValue = value;
                }
            }
        }

        #endregion

        #region Methods

        /// <inheritdoc/>
        void IToDoListView.BindStateFilter(object dataSource, string valueMember, string displayMember)
        {
            _stateToolStripComboBox.ComboBox.ValueMember = valueMember;
            _stateToolStripComboBox.ComboBox.DisplayMember = displayMember;
            _stateToolStripComboBox.ComboBox.DataSource = dataSource;
        }


        /// <inheritdoc/>
        void IToDoListView.ShowItems(IList<ToDo> items)
        {
            foreach (var item in items)
            {
                var lvItem = new ListViewItem(item.Title);
                lvItem.SubItems.Add(item.GetStateText());
                lvItem.SubItems.Add(item.RegisteredDateTime.ToString());
                lvItem.SubItems.Add(item.TouchedDateTime.ToString());
                lvItem.SubItems.Add(item.CompletedDateTime.ToString());
                lvItem.Tag = item;
                _toDoListView.Items.Add(lvItem);
            }
        }

        /// <inheritdoc/>
        void IToDoListView.ClearItems()
        {
            _toDoListView.Items.Clear();
        }

        /// <inheritdoc/>
        void IToDoListView.ShowCreationView(ToDoListFacade facade)
        {
            using (var form = new ToDoCreationForm(facade))
            {
                form.ShowDialog(this);
            }
        }

        /// <inheritdoc/>
        void IToDoListView.ShowEditionForm(ToDoListFacade facade, ToDo target)
        {
            using (var form = new ToDoEditionForm(facade, target))
            {
                form.ShowDialog(this);
            }
        }


        /// <inheritdoc/>
        void IToDoListView.BindCategories(object dataSource, string valueMember, string displayMember)
        {
            _categoryToolStripComboBox.ComboBox.ValueMember = valueMember;
            _categoryToolStripComboBox.ComboBox.DisplayMember = displayMember;
            _categoryToolStripComboBox.ComboBox.DataSource = dataSource;
        }

        /// <inheritdoc/>
        void IToDoListView.ShowCategoryCreationForm(ToDoListFacade facade)
        {
            using (var form = new CategoryCreationForm(facade))
            {
                form.ShowDialog(this);
            }
        }

        /// <inheritdoc/>
        void IToDoListView.ShowCategoryEditionForm(ToDoListFacade facade, Category target)
        {
            using (var form = new CategoryEditionForm(facade, target))
            {
                form.ShowDialog(this);
            }
        }

        #endregion
    }
}
